<template>
</template>

<script>
</script>

<style>
</style><template>
	<el-dialog
	  title="结论"
	  :visible.sync="visible"
	  :before-close="close"
	  :close-on-click-modal="false"
	  width="70%">
    <el-table
    	:data="items"
    	border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column
        prop="checkItemId"
        header-align="center"
        align="center"
        label="编号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="inspectionCategoryName"
        header-align="center"
        align="center"
        label="检验项分类"
        width="100">
      </el-table-column>
      <el-table-column
        prop="inspectionItemName"
        header-align="center"
        align="center"
        label="检验项名称"
        width="100">
      </el-table-column>
      <el-table-column
        prop="inspectionGroupName"
        header-align="center"
        align="center"
        label="检验组"
        width="100">
      </el-table-column>
      <el-table-column
      	prop="insideInspectionItemValue"
      	header-align="center"
      	align="center"
      	label="内控标准值">
        <template slot-scope="{row}">
          <span v-html='row.insideInspectionItemValue'></span>
        </template>
      </el-table-column>
      <el-table-column
      	prop="itemConclusionMerge"
      	header-align="center"
      	align="center"
      	label="检验结果">
      </el-table-column>
      <el-table-column
      	prop="itemVerdict"
      	header-align="center"
      	align="center"
      	label="结果判断"
        width="100">
        <template slot-scope="{row}">
          {{ row.itemVerdict == 0 ? '符合规定' : row.itemVerdict == 1 ? '不符合规定' : null }}
        </template>
      </el-table-column>
      <el-table-column
      	prop="inspectionUserName"
      	header-align="center"
      	align="center"
      	label="检验人"
        width="100">
      </el-table-column>
      <el-table-column
      	prop="startTime"
      	header-align="center"
      	align="center"
      	label="检验开始时间"
        width="110">
        <template slot-scope="{row}">
          {{ row.startTime | changeTime }}
        </template>
      </el-table-column>
      <el-table-column
      	prop="endTime"
      	header-align="center"
      	align="center"
      	label="检验结束时间"
        width="110">
        <template slot-scope="{row}">
          {{ row.endTime | changeTime }}
        </template>
      </el-table-column>
    </el-table>

    <el-card shadow="hover">
      <el-col :span="12">
        <el-form :model="dataForm" label-width="80px">
          <el-form-item label="系统判定:">
            <span v-if="sysVerdict" style="color: #67C23A;">符合规定</span>
            <span v-else style="color: #F56C6C;">不符合规定</span>
          </el-form-item>

          <el-form-item label="人工判定:">
            <el-radio-group v-model="dataForm.verdict" @change="setStandard">
              <el-radio-button :label="0">符合规定</el-radio-button>
              <el-radio-button :label="1">不符合规定</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="内控依据:">
            <el-input type="textarea" autosize v-model="dataForm.accordingToInside" @change="setStandard" placeholder="内控依据" />
          </el-form-item>

          <el-form-item v-if="haveNation" label="国家依据:">
            <el-input type="textarea" autosize v-model="dataForm.accordingToNation" @change="setStandard" placeholder="国家依据" />
          </el-form-item>

          <el-form-item>
            <span v-if="haveNation" slot="label">内部结论:</span>
            <span v-else slot="label">结&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;论:</span>
            <el-input type="textarea" autosize v-model="dataForm.insideVerdict" @change="setStandardByMe" placeholder="内部结论" />
          </el-form-item>

          <el-form-item v-if="haveNation" label="国家结论:">
            <el-input type="textarea" autosize v-model="dataForm.nationVerdict" @change="setStandardByMe" placeholder="国家结论" />
          </el-form-item>

          <el-form-item label="报告日期:">
            <el-date-picker
              v-model="dataForm.reportDate"
              @change="setCheckList"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 100%;">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="物料编码:">
            {{ showCheckList.batchNum }}
          </el-form-item>

          <el-form-item label="报告编号:">
            <el-input v-model="dataForm.checkNum" placeholder="报告编号"></el-input>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :offset="1" :span="11">
        <div class="el-form-item__label"><b>最近历史记录</b></div>
        <el-table
          :data="history"
          border
          fit
          highlight-current-row
          style="width: 100%">
          <el-table-column
            prop="checkNum"
            header-align="center"
            align="center"
            label="报告编号" />
          <el-table-column
            prop="batchNumPrefix"
            header-align="center"
            align="center"
            label="批号" />
        </el-table>
      </el-col>
    </el-card>
	  <span slot="footer" class="dialog-footer">
	    <el-button @click="close">取 消</el-button>
	    <el-button type="primary" class="pan-btn primary-btn" :loading="submiting" @click="submit">确 定</el-button>
  	</span>

    <el-dialog
      title="这份检验单之前一共走了多少批"
      :visible.sync="inputVisible"
      :before-close="closeInput"
      append-to-body
      :close-on-click-modal="false"
      width="20%">
      <el-input v-if="showCheckList.checkTypeId != 7" v-model="dataForm.product.nowSerial.serialNumber" placeholder="流水号" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeInput">取 消</el-button>
        <el-button type="primary" class="pan-btn primary-btn" :loading="submitSerialing" @click="submitSerial">确 定</el-button>
      </span>
    </el-dialog>

	</el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getVerdictInfo,getCheckListsByShowCheckId,verdict,getVersion } from '@/api/check'
  import { updateSerialNum } from '@/api/product'
	import { CODE_OK } from '@/common/js/config'
  import { isPositiveNumber } from '@/common/js/validate'
  import { isBlank,parseTime } from '@/common/js/utils'
	export default {
		data() {
			return {
        printClassify: '报告书',
				visible: false,
        sysVerdict: true,
        haveNation: false,
        isRecall: false,
        dataForm: {
          verdict: '',
          accordingToInside: '',
          accordingToNation: '',
          insideVerdict: '',
          nationVerdict: '',
          printReportVersion: '',
          reportDate: '',
          checkLists: [],
          standards: [],
          checkNum: '',
          product: {
            nowSerial: {
              serialNumber: null
            }
          }
        },
        showCheckList: {},
        items: [],
        history: [],
        versions: [],
        submiting: false,
        inputVisible: false,
        submitSerialing: false
			}
		},
		props: ["childDataForm"],
		created() {
			if (!isBlank(this.childDataForm)) {
        this.showCheckList = this.childDataForm
        this.isRecall = isBlank(this.showCheckList.checkNum) ? false : true
        this.dataForm.checkNum = this.showCheckList.checkNum
        this.dataForm.printReportVersion = this.showCheckList.printReportVersion
        this.getInfo()
        this.visible = true
			}
		},
    computed: {
      ...mapGetters([
         'userInfo'
      ])
    },
		methods: {
      resetCheckNum() {
        for (let i=0,j=this.dataForm.checkLists.length; i<j; i++) {
          this.dataForm.checkLists[i].checkNum = null
        }
        this.getVerdictInfo('checkNum')
      },
      submitSerial() {
        let num = this.dataForm.product.nowSerial.serialNumber
        if (!isPositiveNumber(num)) {
          this.$message.error("请输入正数")
          this.dataForm.product.nowSerial.serialNumber = null
          return
        }
        const data = {
          productSerialId: this.dataForm.product.nowSerial.productSerialId,
          serialNumber: num
        }
        updateSerialNum(data).then(res => {
          if (res.code === CODE_OK) {
            this.$message.success('修改成功')
            this.closeInput('submit')
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      closeInput(val) {
        if (val == 'submit') {
          this.resetCheckNum()
        }
        this.inputVisible = false
      },
      showSerialNumberInput() {
        this.inputVisible = true
      },
      getInfo() {
        const params = {
          showCheckId: this.showCheckList.checkId,
          productId: this.showCheckList.productId
        }
        getCheckListsByShowCheckId(params).then(res => {
          if (res.code === CODE_OK) {
            this.dataForm.checkLists = res.lists
            this.dataForm.reportDate = parseTime(new Date())
            this.setCheckList()
          }else{
          	this.$message.error(res.msg)
          }
        })
        this.getVerdictInfo('all')
        const params2 = {
          checkTypeId:  this.showCheckList.checkTypeId,
          printClassify:  this.printClassify
        }
        getVersion(params2).then(res => {
          if (res.code === CODE_OK) {
            this.versions = res.list.map(v => {
              if (v.isCurrent == 0) {
                this.$set(v, 'title', v.recordNum + '-' + v.versionNum + '(当前版本)')
                if (isBlank(this.dataForm.printReportVersion)) {
                  this.dataForm.printReportVersion = v.versionNum
                }
              }else{
                this.$set(v, 'title', v.recordNum + '-' + versionNum)
              }
              return v
            })
          }else{
            this.$message.error(res.msg)
          }
        })
      },
      // getSerialYearByBatchNumPrefix() {
      //   if (this.wk(this.showCheckList.checkTypeId)) return ""
      //   let batchNumSuffix = this.showCheckList.batchNumPrefix
      //   if (this.yfb(this.showCheckList.checkTypeId) || this.ys(this.showCheckList.checkTypeId)) {
      //     // 取最后8位
      //     batchNumSuffix = batchNumSuffix.substring(batchNumSuffix.length-8)
      //   }else if(this.zc(this.showCheckList.checkTypeId)) {
      //     // “20” + 最后6位
      //     batchNumSuffix = "20" + "" + batchNumSuffix.substring(batchNumSuffix.length-6)
      //   }
      //   // 取前4位
      //   return batchNumSuffix.substring(0, 4)
      // },
      getVerdictInfo(val){
        const params = {
          showCheckId: this.showCheckList.checkId,
          productId: this.showCheckList.productId,
          checkTypeId: this.showCheckList.checkTypeId
		  //,serialYear: this.getSerialYearByBatchNumPrefix()
        }
        getVerdictInfo(params).then(res => {
          if (res.code === CODE_OK) {
            if (val == 'all') {
              this.items = res.info.items
              let inspectDate = this.moment(new Date())
              for (let i=0,j=this.items.length; i<j; i++) {
                inspectDate = this.moment.min(inspectDate, this.moment(this.items[i].startTime))
                if (this.items[i].itemVerdict == 1) {
                  this.sysVerdict = false
                }
              }
              inspectDate = inspectDate.format('YYYY-MM-DD HH:mm:ss')
              this.dataForm.checkLists = this.dataForm.checkLists.map(l => {
                l.inspectDate = inspectDate
                return l
              })
              this.dataForm.standards = res.info.standards
              this.dataForm.accordingToInside = this.dataForm.standards[0].accordingToInside
              this.dataForm.accordingToNation = this.dataForm.standards[0].accordingToNation
              this.haveNation = isBlank(this.dataForm.standards[0].nationInspectionStandardNum) ? false : true
              this.dataForm.product = res.info.product
              if (isBlank(this.dataForm.product.nowSerial)) {
                this.$set(this.dataForm.product,'nowSerial',{
                  serialNumber: null
                })
              }
              this.history = res.info.history
              console.log(this.showCheckList)
              if("中间品" == this.showCheckList.checkTypeName){
                this.dataForm.checkNum = this.showCheckList.productGmp +"-"+ this.showCheckList.batchNum
              }else{
                this.dataForm.checkNum = this.isRecall ? this.dataForm.checkNum : res.info.list.checkNum
              }
            }else if(val == 'checkNum'){
              this.dataForm.checkNum = res.info.list.checkNum
              this.$message.success('重置成功')
            }
          }else{
            this.$message.error(res.msg)
            this.close()
          }
        })
      },
			close(val) {
				this.$emit('close')
        if (val == 'submit') {
          this.dataForm.checkLists[0].isHalf = 1
          this.dataForm.checkLists[0].checkMergeId = this.showCheckList.checkMergeId
          //this.$emit('print', this.dataForm.checkLists[0])
          this.$emit('refreshData')
        }
				this.visible = false
			},
			submit() {
        if (this.formVerify()) {
          this.setCheckList()
          const data = {
            checkMergeId: this.showCheckList.checkMergeId,
            checkLists: this.dataForm.checkLists,
            standards: this.dataForm.standards
          }
          this.submiting = true
          verdict(data).then(res => {
            if (res.code === CODE_OK) {
            	this.$message({
            	  message: '提交成功',
            	  type: 'success',
            	  duration: 1500
            	})
              this.close('submit')
            }else{
            	this.$message.error(res.msg)
              this.submiting = false
            }
          })
        }
			},
      formVerify() {
        if (this.dataForm.verdict != 0 && this.dataForm.verdict != 1) {
          this.$message.error('请进行判定')
          return false
        }
        if (isBlank(this.dataForm.accordingToInside)) {
          this.$message.error('请填写内控依据')
          return false
        }
        if (this.haveNation && isBlank(this.dataForm.accordingToNation)) {
          this.$message.error('请填写国家依据')
          return false
        }
        if (isBlank(this.dataForm.insideVerdict)) {
          this.$message.error('请填写结论')
          return false
        }
        if (this.haveNation && isBlank(this.dataForm.nationVerdict)) {
          this.$message.error('请填写结论')
          return false
        }
        if (isBlank(this.dataForm.reportDate)) {
          this.$message.error('请选择报告日期')
          return false
        }
        if (isBlank(this.dataForm.printReportVersion)) {
          this.$message.error('请选择报告书版本号')
          return false
        }
        if (isBlank(this.dataForm.checkNum)) {
          this.$message.error('请输入报告编号')
          return false
        }else if(this.dataForm.checkNum.indexOf('@') > -1){
          this.$message.error("请修改报告编号，并且不能包含'@'符号")
          return false
        }
        return true
      },
      setCheckNum() {
        if (isBlank(this.dataForm.checkNum) || this.dataForm.checkNum.indexOf('@') > -1) {
          this.$message.error("报告编号不能为空,并且不能包含'@'符号")
          return
        }
        for (let i=0,j=this.dataForm.checkLists.length; i<j; i++) {
          this.dataForm.checkLists[i].checkNum = JSON.parse(JSON.stringify(this.dataForm.checkNum))
        }
      },
      setStandardByMe() {
        for (let i=0,j=this.dataForm.standards.length; i<j; i++) {
          this.dataForm.standards[i].insideVerdict = this.dataForm.insideVerdict
          if (this.haveNation) {
            this.dataForm.standards[i].nationVerdict = this.dataForm.nationVerdict
          }
        }
      },
      setAccording() {
        for (let i=0,j=this.dataForm.standards.length; i<j; i++) {
          this.dataForm.standards[i].accordingToInside = this.dataForm.accordingToInside
          this.dataForm.standards[i].accordingToNation = this.dataForm.accordingToNation
        }
      },
      setStandard() {
        this.setAccording()
        let now = parseTime(new Date())
        let yishangxiangmu = this.showCheckList.checkTypeName == '成品' || this.showCheckList.checkTypeName == '包材' || this.showCheckList.isAllTest == 0 ? '' : '以上项目'
        if (this.dataForm.verdict == 0) {
          this.dataForm.insideVerdict = "本品按" + this.dataForm.standards[0].accordingToInside + "检验" + yishangxiangmu + ",结果符合规定。"
          if (this.haveNation) {
            this.dataForm.nationVerdict = "本品按" + this.dataForm.standards[0].accordingToNation + "检验" + yishangxiangmu + ",结果符合规定。"
          }
        }else{
          this.dataForm.insideVerdict = "本品按" + this.dataForm.standards[0].accordingToInside + "检验" + yishangxiangmu + ",结果不符合规定。"
          if (this.haveNation) {
            this.dataForm.nationVerdict = "本品按" + this.dataForm.standards[0].accordingToNation + "检验" + yishangxiangmu + ",结果不符合规定。"
          }
        }
        for (let i=0,j=this.dataForm.checkLists.length; i<j; i++) {
          if (!isBlank(this.dataForm.standards[i])) {
            this.dataForm.standards[i].verdict = this.dataForm.verdict
            this.dataForm.standards[i].verdictUserId = this.userInfo.userId
            this.dataForm.standards[i].verdictUserName = this.userInfo.name
            this.dataForm.standards[i].verdictDate = now
            this.dataForm.standards[i].insideVerdict = this.dataForm.insideVerdict
            if (this.haveNation) {
              this.dataForm.standards[i].nationVerdict = this.dataForm.nationVerdict
            }
          }
          this.dataForm.checkLists[i].verdictUserId = this.userInfo.userId
          this.dataForm.checkLists[i].verdictUserName = this.userInfo.name
          this.dataForm.checkLists[i].verdictDate = now
        }
      },
      setCheckList(){
        for (let i=0,j=this.dataForm.checkLists.length; i<j; i++) {
          this.dataForm.checkLists[i].reportUserId = this.userInfo.userId
          this.dataForm.checkLists[i].reportUserName = this.userInfo.name
          this.dataForm.checkLists[i].reportDate = this.dataForm.reportDate
          this.dataForm.checkLists[i].printReportVersion = this.dataForm.printReportVersion
		  this.dataForm.checkLists[i].checkNum = this.dataForm.checkNum
        }
      }
    }
	}
</script>

<style scoped>
  >>> .el-form-item__content {
    line-height: 40px !important;
  }
</style>
